package com.zhihuiweijian.demo.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.logging.Logger;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import com.alibaba.fastjson.JSONObject;
import com.zhihuiweijian.demo.config.ApplicationContextRegister;
import com.zhihuiweijian.demo.model.Waste;
import com.zhihuiweijian.demo.service.UserService;
import com.zhihuiweijian.demo.service.WasteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@ServerEndpoint(value = "/websocket")
@Component
@Slf4j
public class WebSocketController {




    /**
     * @throws
     * @Title: onOpen
     * @Description: 连接建立成功调用的方法
     * @param: @param session
     * @return: void
     */
    @OnOpen
    public void onOpen(Session session) {
        System.out.println("连接建立成功调用的方法");
    }

    /**
     * @throws
     * @Title: onClose
     * @Description: 连接关闭调用的方法
     * @param:
     * @return: void
     */
    @OnClose
    public void onClose() {

        System.out.println("连接关闭调用的方法");
    }

    /**
     * @throws
     * @Title: onMessage
     * @Description: 收到客户端消息后调用的方法
     * @param: @param message
     * @param: @param session
     * @param: @throws IOException
     * @return: void
     */
    @OnMessage
    public void onMessage(String message, Session session) throws IOException, ParseException {
        System.out.println("收到客户端消息后调用的方法 ");
        ApplicationContext act = ApplicationContextRegister.getApplicationContext();
        WasteService wasteService = act.getBean(WasteService.class);
        session.getBasicRemote().sendText(message);
        System.out.println(message);
        String[] ws = message.split(" ");
        Integer id = Integer.valueOf(ws[0]);
        Integer hospitalId = Integer.valueOf(ws[1]);
        String hospitalName = ws[2];
        String dept = ws[3];
        String deptManName = ws[4];
        String type = ws[5];
        Double weight = Double.valueOf(ws[6]);
            //获得当前时间
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            Calendar cal = Calendar.getInstance();
            Date time1 = df.parse(df.format(cal.getTime()));

            Waste waste = new Waste(id, hospitalId,hospitalName,dept,deptManName,type, weight,time1);
            System.out.println(waste.toString());
            wasteService.insert(waste);
    }
    /**
     *
     * @Title: onError
     * @Description: 发生错误时调用
     * @param: @param session
     * @param: @param error
     * @return: void
     * @throws
     */
    public void onError (Session session, Throwable error){
        System.out.println("发生错误");
        error.printStackTrace();
    }

}

